<template>
  <div class="app-container">
   <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
    <el-form-item label="业务方" prop="companyName">
       <div class="search-vitem">
           {{orgTenantData.companyName}}
       </div>
     </el-form-item>
    <el-form-item label="小贷机构" prop="orgName">
      <div class="search-vitem">
          {{orgTenantData.orgName}}
      </div>
     </el-form-item>
   
    <!-- <el-form-item>
       <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      
     </el-form-item> -->
   </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:tenant:list']"
        >新增</el-button>
      </el-col>
     
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="业务方商户" align="center" prop="orgName" />
      <el-table-column label="小贷商户" align="center" prop="xdOrgName" />
      <el-table-column label="产品" align="center" prop="productName" />
      <el-table-column label="通道费率" align="center" prop="channelRate" />
	  <el-table-column label="最低通道费" align="center" prop="limitAmount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:tenant:list']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:tenant:list']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="configRef" :model="form" :rules="rules" label-width="120px">
		  <el-form-item label="业务方" prop="orgTenantId" style="display: none;">
		    <el-input v-model="form.orgTenantId" placeholder="" />
		  </el-form-item>
		  <el-form-item label="小贷商户" prop="xdTenantId" style="display: none;">
		    <el-input v-model="form.xdTenantId" placeholder="" />
		  </el-form-item>
        <el-form-item label="业务方" prop="companyName">
          <div class="search-vitem">
              {{orgTenantData.companyName}}
          </div>
        </el-form-item>
        <el-form-item label="小贷商户" prop="orgName">
          <div class="search-vitem">
              {{orgTenantData.orgName}}
          </div>
        </el-form-item>
        <el-form-item label="产品类型" prop="productId">
          <el-tree-select
             v-model="form.productId"
             :data="productOptions"
             :props="{ value: 'id', label: 'productName', children: 'children' }"
             value-key="id"
             placeholder="产品类型"
             check-strictly
          />
		  
        </el-form-item>
        <el-form-item label="通道费率" prop="channelRate">
          <el-input v-model="form.channelRate" placeholder="请输入通道费率" />
        </el-form-item>
		<el-form-item label="最低通道费(元)" prop="limitAmount">
		  <el-input v-model="form.limitAmount" placeholder="请输入最低通道费" />
		</el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Config">
import { listProductConfig, getProductConfig,saveProductConfig } from "@/api/system/productConfig";
import { getOrg } from "@/api/company/org";
import { listProduct } from "@/api/system/product";
const { proxy } = getCurrentInstance();
const route = useRoute();
const configList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const productOptions = ref([]);

const data = reactive({
  orgTenantData: {
	id: null,
  	tenantId: null,
  	companyName: null,
  	orgId: null,
  	orgName: null
  },
  form: {
	orgTenantId: null,
	xdTenantId: null,  
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orgTenantId: null,
    xdTenantId: null,
    productId: null,
    channelRate: null,
  },
  rules: {
    productId: [
      { required: true, message: "产品id不能为空", trigger: "blur" }
    ],
    channelRate: [
      { required: true, message: "通道费率不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules,orgTenantData } = toRefs(data);

/** 查询【请填写功能名称】列表 */
function getDataList() {
  loading.value = true;
  queryParams.value.tenantOrgId=route.params.tenantOrgId;
  listProductConfig(queryParams.value).then(response => {
    configList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

function getList() {
  const _id = route.params.tenantOrgId;
  getOrg(_id).then(response => {
    orgTenantData.value = response.data;
	queryParams.value.orgTenantId=orgTenantData.value.tenantId;
	queryParams.value.xdTenantId=orgTenantData.value.orgId;
	form.value.orgTenantId=orgTenantData.value.tenantId;
	form.value.xdTenantId=orgTenantData.value.orgId;
	getDataList();
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    orgTenantId: orgTenantData.value.tenantId,
    xdTenantId: orgTenantData.value.orgId,
    productId: null,
    channelRate: null,
	limitAmount: null,
  };
  proxy.resetForm("configRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  listProduct().then(response => {
    productOptions.value = proxy.handleTree(response.data, "id");
  });
  open.value = true;
  title.value = "添加【";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  listProduct().then(response => {
    productOptions.value = proxy.handleTree(response.data, "id");
  });
  const _id = row.id || ids.value
  getProductConfig(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改";
  });
}

/** 提交按钮 */
function submitForm() {
  
  proxy.$refs["configRef"].validate(valid => {
	  // form.value.orgTenantId=orgTenantData.tenantId;
	  // form.value.xdTenantId=orgTenantData.orgId;
    if (valid) {
      if (form.value.id != null) {
        saveProductConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        saveProductConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}




getList();
</script>
